package com.wolf.etl.core.transform;

import com.wolf.etl.core.dialect.AbstractDialect;
import com.wolf.etl.model.EtlTableModel;
import com.wolf.etl.model.EtlTransformDefineModel;
import com.wolf.vo.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sdyang
 * @create 2019-06-13 10:08
 **/
@Service("SqlTransform")
public class SqlTransform extends AbstractTransform {

    private static Logger logger = LoggerFactory.getLogger(SqlTransform.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.core.transform#SqlTransform";


    @Override
    public BaseResult doTransform() {
        BaseResult result = BaseResult.getSuccess();

        AbstractDialect srcDialect = AbstractDialect.getInstance(manager,manager.getDataSourceDialet(srcDataSourceId));

        Map<String,String> fieldMap = new HashMap<String,String>();
        for (EtlTransformDefineModel vo : transformDefineModels) {
            fieldMap.put(vo.getSrc_column_name(),vo.getTarget_column_name());
        }
        EtlTableModel srcTable = manager.findEtlTableModel(transformDefineModels.get(0).getSrc_table_id());
        EtlTableModel targetTable = manager.findEtlTableModel(transformDefineModels.get(0).getTarget_table_id());
        String sql = srcDialect.buildInsertSelect(srcTable, targetTable.getName(), fieldMap);

        manager.appendTargetSql(sql);

        result = manager.doTarExecute();

        return result;
    }
}
